<!DOCTYPE html>
<html class="x-admin-sm">
<head>
    <meta charset="UTF-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <link rel="stylesheet" href="/static/xadmin/css/font.css">
    <link rel="stylesheet" href="/static/xadmin/css/xadmin.css">
    <link rel="stylesheet" href="/static/css/apublic.css">
    <script src="/static/xadmin/lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="/static/xadmin/js/xadmin.js"></script>
    <!--[if lt IE 9]>
    <script src="/static/xadmin/html5.min.js"></script>
    <script src="/static/xadmin/respond.min.js"></script>
    <![endif]-->
    <script src="/static/until.js" charset="utf-8"></script>
    <script>
        if(!sessionStorage.getItem('token')) window.top.location.href='/login.html';
    </script>
</head>
<body>
<div class="x-nav">
    <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" onclick="location.reload()" title="刷新">
        <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i>
    </a>
</div>
<div class="layui-fluid check-layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body ">
                    <form class="layui-form layui-col-space5">
                        <div class="layui-inline">
                            <input type="text" name="keyword" placeholder="标题 订单号 收货人 电话" autocomplete="off" class="layui-input">
                        </div>
                        <div class="layui-inline">
                            <input class="layui-input"  autocomplete="off" placeholder="开始日期" name="start" id="start">
                        </div>
                        <div class="layui-inline">
                            <input class="layui-input"  autocomplete="off" placeholder="截止日期" name="end" id="end">
                        </div>
                        <div class="layui-input-inline">
                            <select name="pay">
                                <option value="">支付方式</option>
                                <option value="1">支付宝</option>
                                <option value="2">微信</option>
                                <option value="3">货到付款</option>
                            </select>
                        </div>
                        <div class="layui-input-inline">
                            <select name="status">
                                <option value="">订单状态</option>
                                <option value="1">待付款</option>
                                <option value="2">已付款</option>
                                <option value="3">已发货</option>
                                <option value="4">已完成</option>
                                <option value="5">已作废</option>
                                <option value="6">已退款</option>
                            </select>
                        </div>
                        <div class="layui-inline">
                            <input class="layui-input"  autocomplete="off" placeholder="地区" name="area" id="area">
                        </div>
                        <div class="layui-inline">
                            <button class="layui-btn" lay-submit="" lay-filter="sreach">
                                <i class="layui-icon">&#xe615;</i></button>
                        </div>
                    </form>
                </div>
                <div class="layui-card-body ">
                    <table id="mytable" lay-filter="test"></table>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <!--<button class="layui-btn layui-btn-sm" lay-event="add">添加</button>-->
        <button class="layui-btn layui-btn-sm" lay-event="del">删除选中</button>
    </div>
</script>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="detail">查看</a>
    <!--<a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>-->
</script>
<script>
    var list_url=baseUrl+"/order/list";
    var del_url="/order/del";
    var cols=[ //标题栏
        {type:"checkbox",title:'', width: "5%"},
        {field: 'id', title: 'ID', width: "5%"}
        , {field: 'order_num', title: '订单号', width: "15%"}
        , {field: 'delivery_name', title: '收货人', width: "10%",templet:function(d){
            return d.order_delivery.name
        }}
        , {field: 'money', title: '金额', width: "5%",templet:function(d){
            return d.money.toFixed(2)
        }}
        , {field: 'status', title: '订单状态', width: "10%",templet:function(d){
            if(d.status==1) return "待付款"
            if(d.status==2) return "已付款"
            if(d.status==3) return "已发货"
            if(d.status==5) return "已完成"
            if(d.status==5) return "已作废"
            if(d.status==6) return "已退款"
        }}
        , {field: 'logistics', title: '配送公司', width: "10%",templet:function(d){
            return d.logistics.short_title
        }}
        , {field: 'create_at', title: '下单时间', width: "13%",templet:function(d){
            return utc2datetime(d.create_at)
        }}
        , {field: 'right', title: '操作', width: "10%",toolbar: '#barDemo'}
    ]
    var table;
    layui.use('table', function () {
        table = layui.table;
        table.render({
            elem: '#mytable',
            url: list_url,
            method: 'post',
            headers:{token:sessionStorage.getItem('token')},
            toolbar: '#toolbarDemo',
            page:true,
            parseData: function(res){ //res 即为原始返回的数据
                return {
                    "code": res.status.errorCode, //解析接口状态
                    "msg": res.status.msg, //解析提示文本
                    "count": res.data.total, //解析数据长度
                    "data": res.data.data //解析数据列表
                };
            },
            cols: [cols],
            done: function (res, curr, count) {
                $("table").css("width", "100%");
            },
        });

        //监听事件
        table.on('toolbar(test)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            switch(obj.event){
                case 'add':
                    //layer.msg('添加');
                    xadmin.open('添加',"./add.html",450,600)
                    break;
                case 'del':
                    var data = checkStatus.data;
                    var id=""
                    for(var i=0;i<data.length;i++){
                        id+=data[i].id
                        if(i<data.length-1) id+=","
                    }
                    layer.confirm('真的删除么', function(index){
                        u_post(del_url,{id:id},function(res){
                            if(res.status.errorCode==0){
                                //obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
                                layer.close(index);
                                table.reload('mytable');
                                /*$(".layui-table-main .layui-form-checked").each(function(){
                                    $(this).parent().parent().parent().remove()
                                })*/
                            }
                            else{
                                layer.msg(res.status.msg)
                            }
                        })

                    });
                    break;
            };
        });
        //监听工具条,,行内
        table.on('tool(test)', function(obj){ //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的 DOM 对象（如果有的话）
            if(layEvent === 'detail'){ //查看
                //do somehing
                xadmin.open('查看','./detail.html?id='+data.id,450,600)
            } else if(layEvent === 'del'){ //删除
                layer.confirm('真的删除么<br/>'+data.order_num, function(index){
                    u_post(del_url,{id:data.id},function(res){
                        if(res.status.errorCode==0){
                            //obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
                            layer.close(index);
                            table.reload('mytable');
                        }
                        else{
                            layer.msg(res.status.msg)
                        }
                    })
                });
            } else if(layEvent === 'edit'){ //编辑
                xadmin.open('修改','./edit.html?id='+data.id,450,600)
            }
        });
    });
    //sreach
    layui.use(['form','laydate'], function(){
        var laydate = layui.laydate;
        var form = layui.form;
        //监听提交
        form.on('submit(sreach)', function(data){
            table.reload('mytable', {
                page: {
                    curr: 1 //重新从第 1 页开始
                },
                where: data.field, //设定异步数据接口的额外参数
                //,height: 300
            });
            return false;
        });
        //初始化时间
        //执行一个laydate实例
        laydate.render({
          elem: '#start' //指定元素
        });

        //执行一个laydate实例
        laydate.render({
          elem: '#end' //指定元素
        });
    });
</script>
</html>

